--优惠卷id
--用户id
local voucherId = ARGV[1]
local userId = ARGV[2]
local orderId = ARGV[3]


--库存key
--订单key
local stockKey = 'seckill:stock:'..voucherId
local orderKey = 'seckill:order:' ..userId

--判断库存，判断一人一单
if(tonumber(redis.call('get',stockKey)) <= 0)then
    return 1
end

if(redis.call('sismember',orderKey,userId)== 1) then
    return 2
end

--减库存，存订单用户
redis.call('incrby',stockKey,-1)

redis.call('sadd',orderKey,userId)

--XADD key ID field string [field string ...]
redis.call('xadd','stream.orders','*','userId',userId,'voucherId',voucherId,'id',orderId)

return 0

